<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>
description("Tests that RTCPeerConnection event callbacks are async so that for example close can be called safely. The order of the messages is very important.");

var stream = null;
var pc = null;

function error() {
    testFailed('Stream generation failed.');
    finishJSTest();
}

function getUserMedia(dictionary, callback) {
    try {
        navigator.webkitGetUserMedia(dictionary, callback, error);
    } catch (e) {
        testFailed('webkitGetUserMedia threw exception :' + e);
        finishJSTest();
    }
}

function onStateChange(event) {
    testFailed('onStateChange was called on close.');
}

function onNegotiationNeeded(event) {
    testPassed('onNegotiationNeeded was called.');
    pc.onsignalingstatechange = onStateChange;
    pc.onnegotiationneeded = onNegotiationNeededAgain;
    // There should be no event fired from pc.close().
    pc.close();
    shouldBe("pc.signalingState", "'closed'");
    testPassed('onNegotiationNeeded done.')
    finishJSTest();
}

function onNegotiationNeededAgain(event) {
    testFailed('onNegotiationNeededAgain should never be called.');
}

function gotStream(s) {
    testPassed('gotStream was called.');
    stream = s;

    pc = new RTCPeerConnection();
    pc.onnegotiationneeded = onNegotiationNeeded;

    pc.addStream(stream);
    testPassed('gotStream done.');
}

getUserMedia({audio:true, video:true}, gotStream);

window.jsTestIsAsync = true;
window.successfullyParsed = true;


</script>
</body>
</html>
